var navigationRule = {};
var picturePath;
var progress = 0;
var downloadHttp;
var uploadHttp;
var uploadUrl;
var downloadUrl;

function init() {
	downloadHttp = new Http();
	uploadHttp = new Http();
	
	_initHeader();
	
	var array = new Array();
	
	array.push("Request (http)");
	array.push("Request (https)");
	array.push("Upload");
	array.push("Download");
	
	if(memoryPreference.contains("uploadUrl") == true) {
		uploadUrl = memoryPreference.get("uploadUrl");
	} else {
		uploadUrl = "http://203.235.211.98:7000/test/services/fileUpload";
	}
	
	if(memoryPreference.contains("downloadUrl") == true) {
		downloadUrl = memoryPreference.get("downloadUrl");
	} else {
		downloadUrl = "http://203.235.211.98:7000/test/images/1361183087709.png";
	}
	
	draw_list(array);
}

function setOnListClickHandler(idx) {
	switch(idx) {
		case 0 :
			testRequest();
			
		break;
		
		case 1 :
			testRequest_https();
			
		break;
		
		case 2 :
			testUpload();
			
		break;
		
		case 3 :
			testDownload();
			
		break;
		
		default :

		break;
	}
}

function testRequest() {
	document.getElementById("picture").innerHTML = "";
	navigate("http_request_http");
}

function testRequest_https() {
	document.getElementById("picture").innerHTML = "";
	navigate("http_request_https");
}

/*
 * testUpload
 */
function testUpload() {
	multimedia.getPicture(getPictureSuccessCallback, getPictureErrorCallback);
}

function getPictureSuccessCallback(path) {
	picturePath = path;
	upload();
}

function getPictureErrorCallback(errorMessage) {
	alert("getPictureErrorCallback! Error Message : " + errorMessage);
}

function upload() {
	var entity = {
		"url" : uploadUrl,
		"filePath" : picturePath
	};
	
	uploadHttp.setRequestHeader("id", "alopex");
	
	var option = {
		"message" : "file uploading...",
		"cancelable" : true,
		"cancelCallback" : "showProgressDialogUploadCancelCallback",
		"color" : "white"
	};

	platformUIComponent.showProgressBarDialog(option);
    uploadHttp.setTimeout("120000"); //2분
	uploadHttp.upload(entity, uploadSuccessCallback, uploadErrorCallback, uploadProgressCallback, uploadCancelCallback);
}

function uploadSuccessCallback() {
	platformUIComponent.dismissProgressBarDialog();
	notifySuccess("upload");
}

function uploadErrorCallback(http) {
	platformUIComponent.dismissProgressBarDialog();
	notifyError("upload", "errorCode: " + http.errorCode + "errorMessage: " + http.errorMessage);
}

function uploadProgressCallback(progress) {
	platformUIComponent.setProgress(progress);
}

function uploadCancelCallback() {
	platformUIComponent.dismissProgressBarDialog();
	alert("Uploading is canceled.");
}

function showProgressDialogUploadCancelCallback() {
	uploadHttp.cancelUpload();
	platformUIComponent.dismissProgressBarDialog();
}

/*
 * testDownload
 */
function testDownload() {
	var entity = {
		"url" : downloadUrl
	};
	
	var option = {
		"message" : "file downloading...",
		"cancelable" : true,
		"cancelCallback" : "showProgressDialogDownloadCancelCallback",
		"color" : "white"
	};

	platformUIComponent.showProgressBarDialog(option);
	
    downloadHttp.setTimeout("120000"); //2분
	downloadHttp.download(entity, downloadSuccessCallback, downloadErrorCallback, downloadProgressCallback, downloadCancelCallback);
}

function showProgressDialogDownloadCancelCallback() {
	downloadHttp.cancelDownload();
	platformUIComponent.dismissProgressBarDialog();
}

function downloadSuccessCallback(path_downoload) {
	document.getElementById("picture").innerHTML = "";
	img = document.createElement("img");
	img.width = "200";
	img.height = "200";
	img.src = path_downoload;
	document.getElementById("picture").appendChild(img);
	
	platformUIComponent.dismissProgressBarDialog();
	
	notifySuccess("file path: " + path_downoload + "\ndownload");
}

function downloadErrorCallback(http) {
	platformUIComponent.dismissProgressBarDialog();
	notifyError("download", "error code: " + http.errorCode + " errorMessage: " + http.errorMessage);
}

function downloadProgressCallback(progress) {
	platformUIComponent.setProgress(progress);
}

function downloadCancelCallback() {
	platformUIComponent.dismissProgressBarDialog();
	alert("Downloading is canceled");
}